﻿#include <iostream>
#include <fstream>
#include <Compiler.hpp>
std::u16string inFileName, outFileName;
bool ParseArgs(int argc, wchar_t* argv[])
{
	for (int i(1); i < argc; ++i)
	{
		if (argv[i] == L"-o")
			if (i + 1 != argc)
				outFileName = reinterpret_cast<char16_t*>(argv[++i]);
			else
			{
				std::wcout << (wchar_t*)multilanguageTable[ML_NOOUTPUTFILE] << std::endl;
				return false;
			}
		else
			inFileName = reinterpret_cast<char16_t*>(argv[i]);
	}
	if (inFileName == u"")
	{
		std::wcout << (wchar_t*)multilanguageTable[ML_NOINPUTFILE] << std::endl;
		return false;
	}
	if (outFileName == u"")
		outFileName = inFileName.substr(0, inFileName.find_last_of('.')) + u".wdtoken";
	return true;
}
int wmain(int argc, wchar_t* argv[])
{
	if (!ParseArgs(argc, argv))
		return 1;
	Level5::Preprocessor* preprocessor(new Level5::WaterDropPreprocessor());
	auto tokens((*preprocessor)(inFileName.c_str()));
	std::wofstream of((wchar_t*)outFileName.c_str(), std::ios::out);
	if (of.is_open())
		for (auto token : *tokens)
			of << '(' << token.line << ',' << token.row << ')' << (wchar_t*)Level5::UnicodeConverter::ConvertFromUTF32ToUTF16(token.value).c_str() << std::endl;
	return 0;
}